import { MyRequestTask } from "~/api/my_axios";
// import axios from '~/axios.js'
import chalk from "chalk";
const errorStyle = chalk.whiteBright.bgRedBright.bold;
const infoStyle = chalk.underline;

// 判断键是否有未定义的值,并收集
// 并且去除两端的空格
function checkUndefinedData(_prepare_data) {
  let _undefined_box = [];
  for (let i in _prepare_data) {
    console.log(i,_prepare_data[i],"prepdata");
    if (_prepare_data[i] === undefined) {
      _undefined_box.push(i.toString()); // 放入未给出值的变量 的列表
    }
    // 如果是字符串则使用trim方法
    else if(typeof _prepare_data[i] === 'string'){
      console.log("pre",_prepare_data[i]);
      _prepare_data[i] = _prepare_data[i].trim();
      console.log('end :>> ', _prepare_data[i]);
    }
  }
  console.log("out: _prepare_data",_prepare_data)
  return _undefined_box;
}

export function addCronData(_data, num, unit) {
  let _prepare_data = {
    cron: _data.cron, // "* * * * * ?",
    description: _data.description, // "测试",
    end: _data.end, // "2023-06-29",
    jobType: _data.jobType, // "hello",
    misfireInstruction: _data.misfireInstruction, // 1,
    start: _data.start, // "2023-06-28"
  };
  try {
    const undefined_box = checkUndefinedData(_prepare_data);
    if (Array.isArray(undefined_box) && undefined_box.length === 0) {
      return MyRequestTask.post(`/addCronJob/${num}/${unit}`, _prepare_data, {
        headers: {
          "Content-Type": "application/json",
        },
      });
    } else {
      throw "缺少参数:" + undefined_box.toString();
    }
  } catch (e) {
    // 处理错误
    Promise.reject(e);
  }
}

// 添加Daily时间类型任务
export function addDailyData(_data, num, unit) {
  let _prepare_data = {
    description: _data.description, // "测试",
    end: _data.end, //"2023-07-06",
    expression: _data.expression, //"5 10 30 16 0",
    jobType: _data.jobType, //"hello",
    start: _data.start, //"2023-06-28"
  };
  try {
    const undefined_box = checkUndefinedData(_prepare_data);
    if (Array.isArray(undefined_box) && undefined_box.length === 0) {
      // 是空值,即无未定义的值
      return MyRequestTask.post(`/addDailyJob/${num}/${unit}`, _prepare_data, {
        headers: {
          "Content-Type": "application/json",
        },
      });
    } else {
      throw "缺少参数:" + undefined_box.toString();
    }
  } catch (e) {
    // 处理错误
    Promise.reject(e);
  }
}

// 添加Once时间类型任务
export function addOnceData(_data, num, unit) {
  let _prepare_data = {
    description: _data.description, // "测试",
    jobType: _data.jobType, // "export",
    start: _data.start, // "2023-07-03 23:39:00"
  };
  try {
    console.log(JSON.stringify(_prepare_data));
    const undefined_box = checkUndefinedData(_prepare_data);
    if (Array.isArray(undefined_box) && undefined_box.length === 0) {
      // 是空值,即无未定义的值
      return MyRequestTask.post(`/addOnceJob/${num}/${unit}`, _prepare_data, {
        headers: {
          "Content-Type": "application/json",
        },
      });
    } else {
      throw "缺少参数:" + undefined_box.toString();
    }
  } catch (e) {
    // 处理错误
    Promise.reject(e);
  }
}

////////////  任务
export function getAllWorkData(
  _params = { jobGroupName: "default_job_group" }
) {
  try {
    return MyRequestTask.get("/queryAllJob", {
      jobGroupName: _params.jobGroupName,
    }).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("queryAllJob")));
      throw err;
    });
  } catch (error) {
    // 传递错误
    Promise.reject("request\n" + error);
  }
}

// 修改一个任务的时间
export function updateWorkTimeData(_params) {
  try {
    return MyRequestTask.get(
      `/updateJob`,
      {
        jobName: _params.jobName,
        jobGroupName: _params.jobGroupName,
        jobTime: _params.jobTime,
      },
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    ).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("update")));
      throw err;
    });
  } catch (error) {
    // 传递错误
    Promise.reject("request\n" + error);
  }
}

// 暂停任务
export function pauseWorkData(_params = { jobGroupName: "default_job_group" }) {
  try {
    return MyRequestTask.get(
      `/pauseJob`,
      {
        jobName: _params.jobName,
        jobGroupName: _params.jobGroupName,
      },
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    ).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("pause")));
      throw err;
    });
  } catch (error) {
    // 传递错误
    Promise.reject("request\n" + error);
  }
}

// 恢复任务
export function resumeWorkData(
  _params = { jobGroupName: "default_job_group" }
) {
  try {
    return MyRequestTask.get(
      `/resumeJob`,
      {
        jobName: _params.jobName,
        jobGroupName: _params.jobGroupName,
      },
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    ).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("resume")));
      throw err;
    });
  } catch (error) {
    // 传递错误
    Promise.reject("request\n" + error);
  }
}

// 执行任务
export function triggerWorkData(
  _params = { jobGroupName: "default_job_group" }
) {
  try {
    return MyRequestTask.get(
      `/trigger`,
      {
        jobName: _params.jobName,
        jobGroupName: _params.jobGroupName,
      },
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    ).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("trigger")));
      throw err;
    });
  } catch (error) {
    // 处理错误
    Promise.reject("request\n" + error);
  }
}

// 删除任务
export async function deleteWorkData(
  _params = { jobGroupName: "default_job_group" }
) {
  try {
    return MyRequestTask.get(
      `/deleteJob`,
      {
        jobName: _params.jobName,
        jobGroupName: _params.jobGroupName,
      },
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    ).catch((err) => {
      console.log(errorStyle("throw by " + infoStyle("delete")));
      throw err;
    });
  } catch (error) {
    // 传递错误
    Promise.reject("request\n" + error);
  }
}
